package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.domain.Article;
import org.example.mapper.ArticleMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class ThreadServiceImpl {

    @Resource
    private ArticleMapper articleMapper;

    @Async("taskExecutor")
    public void updateViewCounts(Long id){
        UpdateWrapper<Article> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.setSql("view_counts = view_counts + 1"); // 默认排它锁
        int total = articleMapper.update(null, wrapper);
        log.info("更新阅读数完成,更新结果: {}",total);
    }
}
